package com.karl.mytask_2.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.karl.mytask_2.R;
import com.karl.mytask_2.bean.MyUser;
import com.karl.mytask_2.tools.BaseActivity;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.LogInListener;
import cn.bmob.v3.listener.SaveListener;

/**
 * Created by Karl on 2016/5/20.
 * 登陆界面
 */
public class LoginActivity extends BaseActivity implements View.OnClickListener {
    private TextView mToolBarTitle;
    private ImageButton mBack;

    private TextView mResetPassword;
    private EditText mEmail,mPassword;
    private String email,pass;
    private Button mLoginBtn,mToRegisterBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.login);
        Bmob.initialize(this, "22efbfa97da23a05fdc822d0d48fc912");
        //进行用户对象判断
        MyUser bmobUser = BmobUser.getCurrentUser(this,MyUser.class);
        if (bmobUser != null) {
            //当用户对象不为空的时候直接进入应用
            startActivity(new Intent().setClass(this, MainActivity.class));
            LoginActivity.this.finish();
        }
        initView();
        initEvent();

    }
    private void initView(){
        mToolBarTitle = (TextView) findViewById(R.id.tool_bar2_title);
        mBack = (ImageButton) findViewById(R.id.tool_bar2_back);
        mEmail = (EditText) findViewById(R.id.login_email);
        mPassword = (EditText) findViewById(R.id.login_password);
        mResetPassword = (TextView) findViewById(R.id.login_reset_pass);
        mLoginBtn = (Button) findViewById(R.id.login_btn);
        mToRegisterBtn = (Button) findViewById(R.id.login_to_register_btn);
    }
    private void initEvent(){
        mToolBarTitle.setText("登陆");
        //设置监听事件
        mBack.setOnClickListener(this);
        mResetPassword.setOnClickListener(this);
        mLoginBtn.setOnClickListener(this);
        mToRegisterBtn.setOnClickListener(this);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.tool_bar2_back:
                LoginActivity.this.finish();
                break;
            case R.id.login_reset_pass:
                startActivity(new Intent().setClass(LoginActivity.this, ResetPasswordActivity.class));
                break;
            case R.id.login_btn:
                toLogin();
                break;
            case R.id.login_to_register_btn:
                startActivity(new Intent().setClass(LoginActivity.this,RegisterActivity.class));
                break;
        }
    }

    private void toLogin(){
        getInputData();
        if (email.equals("") || pass.equals("")){
            Toast.makeText(LoginActivity.this, "用户名和密码不能为空", Toast.LENGTH_SHORT).show();
        }else {
            //登陆代码
            MyUser user = new MyUser();
            user.setUsername(email);
            user.setPassword(pass);
            user.login(this, new SaveListener() {
                @Override
                public void onSuccess() {
                    //登陆成功
                    startActivity(new Intent().setClass(LoginActivity.this,MainActivity.class));
                    LoginActivity.this.finish();
                }

                @Override
                public void onFailure(int i, String s) {
                    Toast.makeText(LoginActivity.this, "登录失败,请检查输入！", Toast.LENGTH_SHORT).show();
                    mPassword.setText("");

                }
            });
        }
    }

    private void getInputData(){
        email = mEmail.getText().toString();
        pass = mPassword.getText().toString();
    }
}
